package com.example.testapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonParseException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.testapp.domain.Catalogue;
import com.example.testapp.domain.CataloguePage;
import com.example.testapp.domain.City;
import com.example.testapp.domain.Location;
import com.example.testapp.service.CatalogueService;
import com.example.testapp.service.LocationService;
import com.example.testapp.service.impl.CatalogueServiceImpl;
import com.example.testapp.service.impl.LocationServiceImpl;

public class MyListActivity extends Activity {

	private CatalogueService catalogueService;
	private LocationService locationService;
	ListView listView ;
	
	public MyListActivity() throws JsonParseException, IOException {
		catalogueService = new CatalogueServiceImpl();
		locationService = new LocationServiceImpl();
	}
	
	 @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_list_view_android_example);
         
         // Get ListView object from xml
         listView = (ListView) findViewById(R.id.list);
         
         // Defined Array values to show in ListView
         String[] values = new String[] { "Electornins", 
                                          "Bags",
                                          "Fashion",
                                          "Sports" 
                                         };
 
         // Define a new Adapter
         // First parameter - Context
         // Second parameter - Layout for the row
         // Third parameter - ID of the TextView to which the data is written
         // Forth - the Array of data
 
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
           android.R.layout.simple_list_item_1, android.R.id.text1, values);
 
 
         // Assign adapter to ListView
         listView.setAdapter(adapter); 
         
         // ListView Item Click Listener
         listView.setOnItemClickListener(new OnItemClickListener() {

               @Override
               public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
                 
            	   Intent i = new Intent(MyListActivity.this, LocationActivity.class);                      
   	        	startActivity(i);
            	//DK:
            	//new GetContacts().execute();
                // ListView Clicked item index
               // int itemPosition     = position;
                
                // ListView Clicked item value
                //String  itemValue    = (String) listView.getItemAtPosition(position);
                   
                 // Show Alert 
//                 Toast.makeText(getApplicationContext(),
//                   "Position :"+itemPosition+"  CategoryItem : " +itemValue , Toast.LENGTH_LONG)
//                   .show();
              
               }
 
          }); 
     }
	 
	 /**
	     * Async task class to get json by making HTTP call
	     * */
	    private class GetContacts extends AsyncTask<Void, Void, Void> {
	 
	        @Override
	        protected void onPreExecute() {
	            super.onPreExecute();
	        }
	 
	        @Override
	        protected Void doInBackground(Void... arg0) {
	        	
	        	Location l = new Location();
	        	locationService.initLocation(l);
	        	List<City> cities = locationService.getAllCities();
	        	List<Location> allLocations = new ArrayList<Location>();
	        	List<Location> locations = null;
	        	for(City city : cities) {
	        		locations = locationService.getLocationsForCity(Long.parseLong(city.getId()));
	        		if(locations != null) {
	        			allLocations.addAll(locations);
	        		}
	        	}
	        	List<Catalogue> catalogues = catalogueService.getLatestCatalogues();
	        	List<CataloguePage> allCataloguePages = new ArrayList<CataloguePage>();
	        	List<CataloguePage> cataloguePages = null;
	        	for(Catalogue catalogue : catalogues) {
	        		cataloguePages = catalogueService.getCataloguePagesForCatalogue(Long.parseLong(catalogue.getId()));
	        		if(cataloguePages != null) {
	        			allCataloguePages.addAll(cataloguePages);
	        		}
	        	}
	            return null;
	        }
	 
	        @Override
	        protected void onPostExecute(Void result) {
	            super.onPostExecute(result);
	        }
	    }
}
